<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="5vh"
    width="1400px"
  >
    <div class="header-box">
      <!-- 关键词-->
      <div>
        <el-input
          v-model="queryInfo.keywords"
          clearable
          placeholder="请输入搜索关键字"
          style="width: 180px"
          @change="handleResetPage"
        >
        </el-input>
      </div>
      <!-- 用户搜索 -->
      <div>
        <el-select
          v-model="queryInfo.member_id"
          :loading="searchItem.member_id.loading"
          :remote-method="handleSearchMember"
          clearable
          filterable
          placeholder="请选择用户"
          remote
          reserve-keyword
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="item in searchItem.member_id.options"
            :key="item.member_id"
            :label="item.account + `(${item.nickname})`"
            :value="item.member_id"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-select
          v-model="queryInfo.pickup_method"
          clearable
          placeholder="请选择交付方式"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.pickup_method"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-select
          v-model="queryInfo.status"
          clearable
          placeholder="请选择状态"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.status"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-select
          v-model="queryInfo.refund"
          clearable
          placeholder="请选择退单状态"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.refund"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" @click="handleResetPage">
          搜索
          <i class="el-icon-search el-icon--right"></i>
        </el-button>
      </div>
    </div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        header-cell-class-name="table-header"
        height="100%"
        stripe
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="订单号"
          prop="order_sn"
          width="175"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="下单用户"
          prop="member"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="交付方式"
          prop="pickupMethod.type"
          width="125"
        >
          <template v-slot="{ row }">
            <el-tag>
              {{ row.pickupMethod.type }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="订单总金额"
          prop="order_fee"
          width="125"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="商品金额"
          prop="goods_fee"
          width="125"
        >
        </el-table-column>

        <el-table-column align="center" label="运费" prop="freight" width="125">
        </el-table-column>

        <el-table-column
          align="center"
          label="付款情况"
          prop="payInfo"
          width="125"
        >
        </el-table-column>

        <el-table-column align="center" label="订单状态" width="150">
          <template v-slot="{ row }">
            <el-tag size="mini" type="primary">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="下单时间"
          prop="create_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="更新时间"
          prop="update_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="发货时间"
          prop="taking_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="完结时间"
          prop="complete_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="备注"
          prop="remark"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页-->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>
  </el-dialog>
</template>

<script>
import { orderLists as getList } from "@/api/groupbuyingPeriod";
import { lists as searchMember } from "@/api/member";

export default {
  name: "Order",
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 查询条件
      queryInfo: {
        page: 1,
        rows: 10,
        keywords: "",
        member_id: "",
        pickup_method: "",
        status: "",
        refund: "",
        gbr_id: this.info.gbr_id,
        gbr_period: this.info.period,
      },
      // 总页数
      totalCount: 0,
      // 列表
      dataList: [],
      // 数据
      globalData: {
        title: "",
      },
      // 筛选项
      filterItem: {
        status: [],
        refund: [],
      },
      // 远程搜索项
      searchItem: {
        member_id: {
          loading: false,
          options: [],
        },
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 防抖
      queryId: +new Date(),
      // 弹窗
      isDialog: {
        visible: true,
      },
    };
  },
  created() {
    console.log(this.info, 3333);
    this.globalData.title = "拼团期次订单" + "(" + this.info.period + ")";
    this.getData();
    this.handleSearchMember();
  },
  methods: {
    // 数据
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
          this.filterItem.status = res.data.status_screen;
          this.filterItem.refund = res.data.refund_screen;
          this.filterItem.pickup_method = res.data.pickup_method_screen;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 远程搜索 - 用户
    handleSearchMember(query) {
      this.searchItem.member_id.loading = true;
      searchMember({
        page: 1,
        rows: 10,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.member_id.loading = false;
        this.searchItem.member_id.options = res.data.list;
      });
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    // 关闭
    handleClose() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  min-height: 60vh;
}

::v-deep .el-dialog__body {
  padding: 20px 20px;
}

.header-box {
  display: flex;

  > div {
    margin-right: 10px;
  }
}

.flex-content {
  margin-top: 10px;
}
</style>
